﻿using System;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Automapping;
using Caramel.NHibernate.Castle;
using Caramel.FluentNHibernate;
using Caramel.FluentNHibernate.Conventions;
using Caramel.NHibernate.Membership.Mapping;

namespace Caramel.NHibernate.Membership.Castle
{
	public class MembershipInstaller : IWindsorInstaller
	{
		public MembershipInstaller()
			: this(
				MsSqlConfiguration.
					MsSql2000.
					ConnectionString(c => c.FromConnectionStringWithKey("LocalSqlServer"))
			)
		{
		}

		public MembershipInstaller(IPersistenceConfigurer persistenceConfigurer)
		{
			this.PersistenceConfigurer = persistenceConfigurer;
		}

		public IPersistenceConfigurer PersistenceConfigurer { get; set; }

		public void Install(IWindsorContainer container, IConfigurationStore store)
		{
			var configuration =
				Fluently.
					Configure().
					ConfigureMembership(this.PersistenceConfigurer).
					BuildConfiguration();

			container.
				Install(
					new NHibernateInstaller(configuration)
				);
		}
	}
}
